/*
 * Copyright 2012 Marsrutai.lt
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package lt.marsrutai.mobile.android.adapter;

import java.util.List;

import lt.marsrutai.mobile.android.R;
import lt.marsrutai.mobile.android.activity.intent.StopMapIntentHolder;
import lt.marsrutai.mobile.android.activity.intent.TracksIntentHolder;
import lt.marsrutai.mobile.android.model.Track;
import lt.marsrutai.mobile.android.model.TrackBookmark;
import lt.marsrutai.mobile.android.model.enums.TrackType;
import lt.marsrutai.mobile.android.util.BookmarkControlManager;
import lt.marsrutai.mobile.android.widget.ListViewAdapter;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;


public class TrackAdapter extends ListViewAdapter<Track> {

    private Bundle fragmentState;

    private int separatorPos = -1;
    
    public TrackAdapter(Activity activity, Bundle fragmentState) {
        super(activity, R.layout.tracks_item);
        this.fragmentState = fragmentState;
    }

    @Override
    public View doGetView(int position, View view, ViewGroup parent) {

        if (position == separatorPos) {
            TextView separatorView = (TextView) inflater.inflate(R.layout.list_item_separator, null);
            separatorView.setText(R.string.changed_tracks);
            return separatorView;
        } else {
            position = resolveItemPosition(position);
        }


        final Track track = getItem(position);
        final TracksIntentHolder holder = new TracksIntentHolder(fragmentState);

        if (track != null) {
            TextView trackName = (TextView) view.findViewById(R.id.track_name);
            if (trackName != null) {
                trackName.setText(track.getName());
            }
            ImageButton mapButton = (ImageButton) view.findViewById(R.id.map);
            mapButton.setFocusable(false);
            mapButton.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    StopMapIntentHolder intentHolder = new StopMapIntentHolder(activity);
                    intentHolder.setScopeFolder(holder.getScopeFolder());
                    intentHolder.setTrackId(track.getId());
                    intentHolder.setScheduleName(holder.getScheduleName());
                    intentHolder.setTransportId(holder.getTransportId());
                    intentHolder.setTitle(track.getName());
                    activity.startActivity(intentHolder.getIntent());
                }
            });

            TrackBookmark trackBookmark = new TrackBookmark();
            trackBookmark.setScopeFolder(holder.getScopeFolder());
            trackBookmark.setScopeName(holder.getScopeName());
            trackBookmark.setScheduleId(track.getScheduleId());
            trackBookmark.setScheduleName(holder.getScheduleName());
            trackBookmark.setScheduleTrackName(holder.getScheduleTrackName());
            trackBookmark.setTrackId(track.getId());
            trackBookmark.setTrackName(track.getName());
            trackBookmark.setTransportId(holder.getTransportId());
            
            BookmarkControlManager.setUpControls(view.findViewById(R.id.star_on), view.findViewById(R.id.star_off), trackBookmark);
        }
        return view;
    }

    public void setTracks(List<Track> tracks) {
        if (null == tracks) {
            return;
        }

        for (int i = 0; i < tracks.size(); i++) {
            add(tracks.get(i));

            if (separatorPos == -1 && TrackType.CHANGED.equals(tracks.get(i).getTrackType())) {
                separatorPos = i;
            }
        }
    }

    @Override
    public int getCount() {
        return super.getCount() + (isSeparatorNeeded() ? 1 : 0);
    }

    private boolean isSeparatorNeeded() {
        return separatorPos > 0;
    }

    public int resolveItemPosition(int position) {
        if (isSeparatorNeeded()) {
            if (position > separatorPos) {
                return --position;
            } else if (position == separatorPos) {
                return -1;
            }
            else {
                return position;
            }
        }
        else {
            return position;
        }
    }
}
